package com.bondar.task5;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class AccountWithLock {
	private int balance;
	private int accountNumber;
	private final Lock lock;

	public AccountWithLock(int accountNumber, int amount) {
		this.accountNumber = accountNumber;
		this.balance = amount;
		this.lock = new ReentrantLock();
	}

	public int getAccountNumber() {
		return accountNumber;
	}
	
	public Lock getLock() {
		return lock;
	}

	public int getBalance() {
		lock.lock();
		try {
			return balance;
		} finally {
			lock.unlock();
		}
	}

	public boolean withdraw(int amount) {
		if (balance >= amount) {
			this.balance = balance - amount;
			return true;
		} else {
//			System.err.println("Insufficient funds");
			return false;
		}
	}

	public void deposit(int amount) {
		this.balance += amount;
	}
}
